const path = require('path')
const fs = require('fs')

module.exports = function(plop) {

  const root = process.cwd()
  const config_path = path.join(root, 'config.js')
  const hasConfig = fs.existsSync(config_path)
  if (!hasConfig) throw new Error('未找到配置文件，请确认是否在根目录下执行')

  const { style: syt } = require(config_path)
  
  plop.setGenerator('component', {
    description: 'create a component',
    prompts: [
      {
        type: 'input',
        name: 'name',
        message: 'component name'
      }
    ],
    actions: [
      {
        type: 'add',
        path:  root + '/src/components/{{name}}/index.js',
        templateFile: '../templates/plop-templates/component_js.hbs'
      },
      {
        type: 'add',
        path:  root + '/src/components/{{name}}/index.' + syt,
        templateFile: '../templates/plop-templates/component_css.hbs'
      },
      {
        type: 'add',
        path:  root + '/src/components/{{name}}/index.html',
        templateFile: '../templates/plop-templates/component_html.hbs'
      }
    ]
  })
}